﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Models;
using GDGeek;
using System;
using NormalBattleIoc;
using UnityEngine.SceneManagement;

public class PlayerManager : Singleton<PlayerManager>
{
    public const string PlayerOneOperation = "角色1选择策略状态";
    public const string PlayerTwoOperation = "角色2选择策略状态";
    public const string PlayerThreeOperation = "角色3选择策略状态";
    public const string System_DoAction = "系统自动轮询执行策略状态";
    public const string System_Wait = "系统初始等待状态";

    //状态转变发送的key
    public const string System_Wait_ChangeTo_PlayerOneOperation = "系统初始等待状态z转换为z角色1选择策略状态";
    public const string PlayerOneOperation_ChangeTo_System_DoAction = "角色1选择策略状态z转换为z系统自动轮询执行策略状态";
    public const string System_DoAction_ChangeTo_System_Wait = "系统自动轮询执行策略状态z转换为z系统初始等待状态";

    public GameObject playerA;
    public GameObject playerB;
    public GameObject playerC;

    public List<PlayerInBattle> playerInbattleList = new List<PlayerInBattle>();

    public FSM fsm_playermanager = new FSM();

    private Vector3 select_target_Pos;

    private TurnBattleEventView eventview;

    public void Set_Select_Target_Pos(Vector3 _p)
    {
        select_target_Pos = _p;

    }

    public Vector3 Get_Select_Target_Pos()
    {
        return select_target_Pos;
    }


    private string stage_playerManager;
    public string Get_NowPlayerManagerStage()
    {
        return stage_playerManager;
    }
    
    //自动状态，，
    //等待玩家A操作状态，，
    //等待玩家B操作状态，，
    //等待玩家C操作状态，，
    //操作结束，，
    private void Awake()
    {
        playerA.GetComponent<SpriteRenderer>().enabled = false;
        playerB.GetComponent<SpriteRenderer>().enabled = false;
        playerC.GetComponent<SpriteRenderer>().enabled = false;
    }



    // Use this for initialization
    void Start()
    {
        SetPlayerData();
        FindTurnBattleEventView();
        Load_And_SetPlayerData();
        InitFSM();
        StartCoroutine(ToPlayerOneOperation());
    }

    IEnumerator ToPlayerOneOperation()
    {
        yield return new WaitForSeconds(0.5f);
        FSM_System_Wait_ChangeTo_PlayerOneOperation();
    }

    void SetPlayerData()
    {
        GamePerson person = GameDataManager.Instance.GetGamePerson();

        PersonBase pbase = person.pbase;
        ResistanceAbility ability = person.ability;
        PersonPoint ppoint = person.point;

        PlayerInBattle  p =  playerA.GetComponent<PlayerInBattle>();


        p.NowHp = pbase.NowHp;
        p.MaxHp = pbase.MaxHp;

        int zhuangBeiAttack = CommonHelper.GetZhuangBeiAttack();

        int zhuangBeiFangwei = CommonHelper.GetZhuangBeiFangWei();

        p.Attack = ppoint.Attack + zhuangBeiAttack;

        p.FangWei = ppoint.FangYu + zhuangBeiFangwei;

        p.Speed = ability.Speed;

        p.fire_Name = FireDic.GetFirePrefabName(  CommonHelper.GetZhuangBeiAttackFirePrefabName()  );

    }




    void FindTurnBattleEventView()
    {
        GameObject g = GameObject.Find("TurnBattleEventView");

        eventview = g.GetComponent<TurnBattleEventView>();
    }

    public GameObject red_tank;
    int index_key = 0;
    public void AddPersonAttackEnemyAction()
    {

       string scene_name =   SceneManager.GetActiveScene().name;

        
     
        if (  stage_playerManager == PlayerOneOperation)
        {
            //添加攻击的 DoAction 到 Action列表里面
            BattleAction action = new BattleAction();
            action.targetPos = select_target_Pos;
            action.actiontype = BattleActionType.PersonAttack;
            //action.next_trun_speed = 5; //当前这个玩家的速度
            action.owner = playerA ;   //当前选择操作的玩家
            PlayerInBattle   pb = playerA.GetComponent<PlayerInBattle>();
            action.attack_point = pb.Attack;
            action.person_long_range_attack_fireName =  pb.fire_Name;  // "Fire_DanGong";
            action.next_trun_speed =  playerA.GetComponent<PlayerInBattle>().Speed  ;

            TurnSystem.Instance.AddAction(   action  );

           // TurnSystem.Instance.WaitSetEnemyAction();
        }

        //转为系统操作状态，，自动开始执行各个action
        FSM_PlayerOneOperation_ChangeTo_System_DoAction();

        if (scene_name == "LaDuoDong2FBattle")
        {
            //添加红狼 的攻击策略......
            if (index_key == 0)
            {
                index_key++;
                return;

            }
            //添加攻击的 DoAction 到 Action列表里面
            BattleAction action = new BattleAction();
            action.targetPos = select_target_Pos;
            action.actiontype = BattleActionType.PersonAttack;

            action.attack_point = 138;

            action.owner = red_tank;   //当前选择操作的玩家
            action.person_long_range_attack_fireName = "Fire_RedTank";
            action.next_trun_speed = 1;

            TurnSystem.Instance.AddAction(action);
        }


    }


    public void AddTankZhuPaoAttackEnemyAction()
    {

       
        if (stage_playerManager == PlayerOneOperation)
        {
            //添加攻击的 DoAction 到 Action列表里面
            BattleAction action = new BattleAction();
            action.targetPos = select_target_Pos;
            action.actiontype = BattleActionType.TankZhuPaoAttack;
            //action.next_trun_speed = 5; //当前这个玩家的速度
            action.owner = playerA;   //当前选择操作的玩家
            action.person_long_range_attack_fireName = "Fire_Tank45Pao";
            action.next_trun_speed = playerA.GetComponent<PlayerInBattle>().Speed;

            TurnSystem.Instance.AddAction(action);

            // TurnSystem.Instance.WaitSetEnemyAction();
        }

        //转为系统操作状态，，自动开始执行各个action
        FSM_PlayerOneOperation_ChangeTo_System_DoAction();

       

    }




    public void OpenPerson_OperationPanel()
    {
        eventview.OpenPerson_OperationPanel();
    }

    public void CloseAll_Panel()
    {
        eventview.CloseAll_Panel();
    }


    void Load_And_SetPlayerData()
    {

        GamePerson person = GameDataManager.Instance.GetGamePerson();

        if (person!=null)
        {
            playerA.GetComponent<SpriteRenderer>().enabled = true;

            PlayerInBattle p = playerA.GetComponent<PlayerInBattle>();
            playerInbattleList.Add(p);
        }
           
    }

    void InitFSM()
    {
        fsm_playermanager.addState(   PlayerOneOperation  , OnPlayerOneOperation());
        fsm_playermanager.addState (  PlayerTwoOperation , OnPlayerTwoOperation());
        fsm_playermanager.addState(   PlayerThreeOperation  , OnPlayerThreeOperation());
        fsm_playermanager.addState(  System_DoAction,  OnSystem_DoActionState( )  );
        fsm_playermanager.addState(System_Wait, OnSystem_Wait());

        fsm_playermanager.init(System_Wait);
    }

    private State OnSystem_Wait()
    {
        StateWithEventMap state = new StateWithEventMap();

      
        state.onStart += delegate
        {

            stage_playerManager = state.name;
            StartCoroutine(ToPlayerOneOperation());
            // FSM_System_Wait_ChangeTo_PlayerOneOperation();
            Debug.Log("进入" + state.name + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + state.name + "状态");
        };
        state.addAction(System_Wait_ChangeTo_PlayerOneOperation, PlayerOneOperation);


        return state;
    }

    private State OnPlayerThreeOperation()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {

            stage_playerManager = state.name;

            Debug.Log("进入" + state.name + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + state.name + "状态");
        };

      
        return state;
    }

    private State OnPlayerTwoOperation()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {

            stage_playerManager = state.name;

            Debug.Log("进入" + state.name + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + state.name + "状态");
        };

        //state.addAction("转换入角色普通攻击状态", "普通攻击");

        return state;
    }

    private State OnPlayerOneOperation()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {

            stage_playerManager = state.name;
            //打开 角色 选择 策略面板
            //这里先要判断 角色的状态,,如果是坐坦克状态，，或，，人物状态
            ChekcPlayer_IsDriveShowUIPanel();
            Debug.Log("进入" + state.name + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + state.name + "状态");
        };

        state.addAction(PlayerOneOperation_ChangeTo_System_DoAction, System_DoAction);


        return state;
    }

    /// <summary>
    /// 检查角色是否驾驶坦克，来判定具体要显示的面板
    /// </summary>
    void ChekcPlayer_IsDriveShowUIPanel()
    {
        PlayerInBattle p1 = playerA.GetComponent<PlayerInBattle>();

         if(   p1.isDrive)
        {
            Debug.Log("ssss");
            eventview.OpenTank_OperationPanel();
        }
        else
        {
            eventview.OpenPerson_OperationPanel();
        }
    }

    private State OnSystem_DoActionState()
    {
        StateWithEventMap state = new StateWithEventMap();
        state.onStart += delegate
        {

            stage_playerManager = state.name;
            TurnSystem.Instance.NextAction();
            Debug.Log("进入" + state.name + "状态");
        };


        state.onOver += delegate
        {
            Debug.Log("退出" + state.name + "状态");
        };

        state.addAction(System_DoAction_ChangeTo_System_Wait,    System_Wait );

        return state;
    }

    public void FSM_System_DoAction_ChangeTo_System_Wait()
    {
        fsm_playermanager.post(  System_DoAction_ChangeTo_System_Wait  );
    }
    public void FSM_PlayerOneOperation_ChangeTo_System_DoAction()
    {
        fsm_playermanager.post(PlayerOneOperation_ChangeTo_System_DoAction);
    }
    public void FSM_System_Wait_ChangeTo_PlayerOneOperation()
    {
        fsm_playermanager.post(System_Wait_ChangeTo_PlayerOneOperation);
    }

    // Update is called once per frame
    void Update()
    {

    }


    public void BackSetPlayerHp()
    {

        GamePerson person = GameDataManager.Instance.GetGamePerson();

        person.pbase.NowHp = playerInbattleList[0].NowHp;


        person.pbase.Exp += 100;
    
    }

}
